#!/usr/bin/env python3


import os
import sys
import warnings
import time
from threading import Timer
import RPi.GPIO as GPIO
from time import sleep
import signal
import subprocess

sleep(2)
led1 = open('/sys/class/leds/led0/brightness', 'wb', 0)
led2 = open('/sys/class/leds/led1/brightness', 'wb', 0)

GND_PIN = 6
LED_PIN = 12
LED_PIN_BCM = 18
SWITCH_PIN = 18
SWITCH_PIN_BCM = 24
LOOP_IN = 16
LOOP_IN_BCM = 23
LOOP_OUT = 22
NC_PIN = 24

class Led():
    def __init__(self):
        self.led_pin = LOOP_IN
        GPIO.setmode(GPIO.BCM)
        GPIO.setwarnings(False)
        GPIO.setup(self.led_pin,GPIO.IN)
        GPIO.setup(self.led_pin,GPIO.OUT,initial=GPIO.HIGH)
        self.press_times=0
        # GPIO.setup(self.led_pin,GPIO.OUT)
        # GPIO.output(self.led_pin,GPIO.LOW)

    def on(self):
        led2.write(b'1')#turn off
        led1.write(b'1')#turn off
        # GPIO.output(self.led_pin,GPIO.HIGH)
        print('LED-ON')

    def off(self):
        led2.write(b'0')#turn on
        led1.write(b'0')#turn on
        # GPIO.output(self.led_pin,GPIO.LOW)
        print('LED-OFF')


def main():
    led = Led()
    # if status == 'on':
    #     led.on()
    # elif status == 'off':
    #     led.off()
    print('-----------')

    while True:        
        # if GPIO.input(led.led_pin):
        #     print('11111111111')
        # else:
        #     print('000000000000')
        if GPIO.input(led.led_pin) == 0:
            led.press_times+=1
        elif led.press_times > 0:
            if led.press_times<10:
            #4为我自己设置的长短按钮分界线，大家可以根据自己的按按钮习惯通过代码自己设定
                print("短按按钮")
            else:
                print("长按按钮")
                led.off()
                p1 = subprocess.getoutput("pgrep record ")
                subprocess.getoutput("kill %s " % p1)
                print("关闭record")
                time.sleep(1)
                # p = subprocess.getoutput("pgrep ./rosbag.sh ")
                # subprocess.getoutput("kill %s " % p)
                # print("关闭./rosbag.sh")
                # time.sleep(1)
                # p3 = subprocess.getoutput("pgrep rplidarNode ")
                # subprocess.getoutput("kill %s " % p3)
                # print("关闭 rplidarNode")
                # time.sleep(1)
                # p4 = subprocess.getoutput("pgrep sensor_imu.py ")
                # subprocess.getoutput("kill %s " % p4)
                # print("关闭 sensor_imu.py")
                # time.sleep(1)
                # p5 = subprocess.getoutput("pgrep roslaunch ")
                # subprocess.getoutput("kill %s " % p5)
                # print("关闭 roslaunch")
                # p9 = subprocess.getoutput("pgrep roslaunch ")
                # subprocess.getoutput("kill %s " % p9)
                # print("关闭 roslaunch")
                # time.sleep(1)
                # p7 = subprocess.getoutput("pgrep static_transform_publish ")
                # subprocess.getoutput("kill %s " % p7) 
                # print("关闭 static_transform_publish") 
                # time.sleep(1)   
                
                # p2 = subprocess.getoutput("pgrep rosbag ")
                # subprocess.getoutput("kill %s " % p2)
                # print("关闭 rosbag")
                # time.sleep(1)  
                # p8 = subprocess.getoutput("pgrep closeros.py ")   
                # subprocess.getoutput("kill %s " % p8)
                # print("关闭 closeros.py")
                # time.sleep(1)   
                # p6 = subprocess.getoutput("pgrep rosmaster ")
                # subprocess.getoutput("kill %s " % p6)
                # print("关闭 rosmaster")
                # time.sleep(3)    
                os.system("sudo poweroff")      
                os.system("sudo shutdown now -P")
                print("关闭计算机") 
                #os.system("shutdown -t 5 now")
                
                time.sleep(3) 
                sys.exit()
                os.system("poweroff")   
		
            led.press_times = 0
        time.sleep(.1)  


if __name__ == '__main__':
    # main(sys.argv[1])
    try:
        main()  
    except KeyboardInterrupt:
        print('User press Ctrl+c, exit;') 
    finally:
        GPIO.cleanup()  
